﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BlogPlat.Controllers;
using BlogPlat.Areas.Admin.Controllers;
using System.Web.Mvc;
using System.Collections;
using BlogPlat.Tests.Models;
using BlogPlat.Model;
using AdminCategoryController = BlogPlat.Areas.Admin.Controllers.CategoryController;
using BlogPlat.Tests.Helpers;

namespace BlogPlat.Tests.Controllers
{
    /// <summary>
    /// Summary description for CategoryControllerTest
    /// </summary>
    [TestClass]
    public class AdminCategoryControllerTest
    {
        public AdminCategoryControllerTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void Admin_CategoryController_Index_Returns_All_Categories()
        {
            // Arrange
            var fakeCategoryRepository = new FakeCategoryRepository();
            List<Category> categories = Mock.CreateDummyCategoryData();
            foreach (Category c in categories)
                fakeCategoryRepository.Add(c);
            var adminCategoryController = new AdminCategoryController(fakeCategoryRepository);            

            // Act
            var result = (ViewResult)adminCategoryController.Index();
           
            // Assert
            CollectionAssert.AllItemsAreInstancesOfType((ICollection)result.ViewData.Model, typeof(Category)); 
        }

        [TestMethod]
        public void Admin_Can_Create_Category()
        {
            // Arrange
            var fakeCategoryRepository = new FakeCategoryRepository();
            var controller = new CategoryController(fakeCategoryRepository);            
            var categoryToCreate = new Category();
            categoryToCreate.CategoryID = 34;
            categoryToCreate.CategoryName = "Cat aA";
            categoryToCreate.FriendlyUrl = "ewdds-asdasd";

            // Act
            controller.Create(categoryToCreate);
            var result = (ViewResult)controller.Index();
           
            // Assert
            var firstEntry = (Category)((IList)result.ViewData.Model)[0];
            Assert.AreEqual(firstEntry, categoryToCreate);
        }

        [TestMethod]
        public void Admin_Can_Delete_Category()
        {
            // Arrange
            var fakeCategoryRepository = new FakeCategoryRepository();
            var controller = new CategoryController(fakeCategoryRepository);
            var categories = Mock.CreateDummyCategoryData();
            var category = categories[2];

            // Act
            foreach (Category cat in categories)
                controller.Create(cat);

            controller.Delete(category.CategoryID);

            // Assert
            Assert.IsNull(fakeCategoryRepository.Get(category.CategoryID));
        }

        [TestMethod]
        public void Admin_Can_Update_Category()
        {
            // Arrange
            var fakeCategoryRepository = new FakeCategoryRepository();
            var controller = new CategoryController(fakeCategoryRepository);
            var categories = Mock.CreateDummyCategoryData();

            // Act
            foreach (Category cat in categories)
                controller.Create(cat);

            var category = (Category)((IList)((ViewResult)controller.Index()).ViewData.Model)[2];
            category.CategoryName = "Updated Category";
            
            // Assert
            Assert.AreEqual(fakeCategoryRepository.List().ToList()[2].CategoryName, "Updated Category");
        }
    }
}
